import request, { ApiResponse } from '@/utils/service/request'
import { OperateAction } from '@/api/types'
import {
  InsertMenuReq,
  MenuItem,
  ModifyMenuReq,
} from '@/api/userCenter/system/model/menuModel'
import { filterPaiXinSysRouterInfo } from '@/utils/routeHelper'

export enum MenuApi {
  LIST = '/user-center/perm/getAll',
  INSERT_MENU = '/user-center/perm/insert',
  MODIFY_MENU = '/user-center/perm/update',
  DELETE_MENU = '/user-center/perm/delete',
  OPERATE = '/menu/operate',
}

export function fetchMenuList(appID?: string) {
  return request
    .get<ApiResponse<MenuItem[]>>(MenuApi.LIST)
    .then((res) => filterPaiXinSysRouterInfo(res, appID))
}

export function fetchAllMenu() {
  return request.get<ApiResponse<MenuItem[]>>(MenuApi.LIST)
}

export function operateMenu(
  body: Partial<MenuItem> & { action: OperateAction }
) {
  return request.post<ApiResponse<null>>(MenuApi.OPERATE, body)
}

export function insertMenu(body: InsertMenuReq) {
  return request.post<ApiResponse<null>>(MenuApi.INSERT_MENU, body)
}

export function modifyMenu(body: ModifyMenuReq) {
  return request.post<ApiResponse<null>>(MenuApi.MODIFY_MENU, body)
}

export function delMenu(body: { id: string }) {
  return request.post<ApiResponse<null>>(MenuApi.DELETE_MENU, body)
}
